<?php
namespace app\common\model;

use think\Exception;

class LiveGoods extends Common
{
    protected $autoWriteTimestamp = true;
    protected $createTime = 'ctime';
    /**
     * 添加、移除宝贝
     * @param $data
     */
    public function doneGoods($data){
        try{
            $goods=$this->where(['uid'=>$data['uid'],'room_gid'=>$data['rg_id']])->find();
            if($goods){
                $res=$goods->delete();
            }else{
                $room_id=Room::where('uid',$data['uid'])->value('id');
                $res=$this->create([
                    'room_id'=>!empty($room_id)?$room_id:0,
                    'room_gid'=>$data['rg_id'],
                    'g_id'=>$data['id'],
                    'uid'=>$data['uid'],
                    'status'=>1,
                    'ctime'=>time()
                ]);
            }
            if($res){
                return [true,'操作成功'];
            }else{
                return [false,'操作失败'];
            }
        }catch (Exception $e){
            return [false,$e->getMessage()];
        }
    }
    /**
     * 获取直播宝贝橱窗列表
     * @param $data
     */
    public function goodsList($data){
        $list=$this->alias('a')
            ->join('zp_goods g','a.g_id=g.id')
            ->field('a.*,g.id,g.name,g.price,g.fee,g.image_id')
            ->where('a.uid',$data['uid']);
        $list=$list->page($data['page'],$data['limit'])->select();
        $total=$list->count();
        foreach ($list as $k=>$v){
            $list[$k]['thumb']=_sImage($v['image_id']);
        }
        return ['count'=>$total,'list'=>$list];
    }
    /**
     * 获取宝贝橱窗宝贝
     */
    public function roomGoods($data){
        $maps[]=['g.uid','=',$data['uid']];
        if(!empty($data['key'])){
            $maps[]=['a.name|a.brief','like','%'.$data['key'].'%'];
        }
        $list=Goods::alias('a')
              ->join('zp_room_goods g','g.g_id=a.id')
              ->join('zp_live_goods l','l.room_gid=g.id','left')
              ->field('a.*,g.id as r_g_id,l.id as room_gid')->where($maps);
        $list=$list->page($data['page'],$data['limit'])->select();
        $total=$list->count();
        foreach ($list as $k=>$v){
            $list[$k]['is_add']=0;
            $list[$k]['thumb']=_sImage($v['image_id']);
            if(!empty($v['room_gid'])){
                $list[$k]['is_add']=1;
            }
        }
        return ['count'=>$total,'list'=>$list];
    }
}
